const router = require("koa-router")();
const Mock = require("mockjs");
const baseUrl = "http://124.221.74.149:3000";
const sizeRooms = ['一居','两居','三居']
const personList = ["李兰", "黄明", "昭武", "李四"];
const levels =  ['毛坯', "精装", "简装"]
const roomList = [
  {
    id: "1",
    url: "/images/9.jpg",
    name: "宣桥枫庭",
    title: "近地铁, 17年建工开发, 交通便利, 小区中间位",
    size: 84.2,
    price: 12346,
    person: "李兰",
    sizeRoom: '一居',
    state: "1",
    level: "毛坯"
  },
  {
    id: "2",
    url: "/images/10.jpg",
    name: "惠城大厦",
    title: " 急卖一百六十W 进才中学旁 经典小户型 中间楼",
    size: 200,
    price: 21334.2,
    person: "黄明",
    sizeRoom: '两居',
    state: "2",
    level: "精装"
  },
];


new Array(10).fill().map((v, i) => {
  roomList.push(
    Mock.mock({
      id: "@id",
      url: `/images/${i + 1}.jpg`,
      name: "@cword(4,6)",
      title: "@csentence",
      size: "@integer(30, 200)",
      price: "@integer(5000,20000)",
      "person|1": personList,
      "state|1": ["1", "2"],
      "sizeRoom|1": sizeRooms,
      "level|1": levels
    })
  );
});

roomList.forEach((v) => (v.url = `${baseUrl}${v.url}`));

/**
 * @swagger
 * /room/list:
 *   get:
 *     summary: 获取房屋列表
 *     description: 获取房屋列表
 *     tags:
 *       - 房屋管理
 *     parameters:
 *       - name: pageCode
 *         in: query
 *         required: false
 *         description: 当前页码
 *         type: string
 *         default: 1
 *       - name: pageSize
 *         in: query
 *         required: false
 *         description: 每页个数
 *         type: string
 *         default: 10
 *       - name: id
 *         in: query
 *         required: false
 *         description: 关键id搜索
 *         type: string
 *       - name: name
 *         in: query
 *         required: false
 *         description: 按照小区名称搜索
 *         type: string
 *       - name: title
 *         in: query
 *         required: false
 *         description: 按照标题搜索
 *         type: string
 *       - name: person
 *         in: query
 *         required: false
 *         description: 按照姓名搜索  "李兰", "黄明", "昭武", "李四"
 *         type: string
 *       - name: state
 *         in: query
 *         required: false
 *         description: 1 已经出售  2 未出售
 *         type: string
 *       - name: level
 *         in: query
 *         required: false
 *         description: 选项：毛坯, 精装, 简装
 *         type: string
 *       - name: sizeRoom
 *         in: query
 *         required: false
 *         description: 选项： 一居 ，两居，三居
 *         type: string
 *     responses:
 *       10000:
 *         description: 成功获取
 */
router.get("/room/list", (ctx) => {
  const {
    id,
    name,
    title,
    person,
    state,
    pageCode = 1,
    pageSize = 10,
    sizeRoom,
    level
  } = ctx.request.query;
  let newRoomList = [...roomList];
  if (id) {
    newRoomList = newRoomList.filter((v) => v.id === id);
  }
  if (name) {
    newRoomList = newRoomList.filter((v) => v.name.includes(name));
  }

  if (title) {
    newRoomList = newRoomList.filter((v) => v.title.includes(title));
  }

  if (person) {
    newRoomList = newRoomList.filter((v) => v.person === person);
  }

  if (state) {
    newRoomList = newRoomList.filter((v) => v.state === state);
  }

  if (sizeRoom) {
    newRoomList = newRoomList.filter((v) => v.sizeRoom === sizeRoom);
  }

  if (level) {
    newRoomList = newRoomList.filter((v) => v.level === level);
  }


  ctx.body = {
    code: 10000,
    data: {
      list: newRoomList.slice((pageCode - 1) * pageSize, pageCode * pageSize),
      total: roomList.length,
    },
    message: "数据获取成功",
  };
});

/**
 * @swagger
 * /room/list:
 *   delete:
 *     summary: 删除列表项目
 *     description: 删除列表项目
 *     tags:
 *       - 房屋管理
 *     parameters:
 *       - name: id
 *         in: query
 *         required: false
 *         description: 删除项目id， 多个id使用逗号隔开
 *         type: string
 *     responses:
 *       10000:
 *         description: 成功获取
 */
router.delete("/room/list", (ctx) => {
  const { id } = ctx.request.query;
  const index = roomList.findIndex((v) => v.id === id);
  if (index > -1) {
    roomList.splice(index, 1);
    ctx.body = {
      code: 10000,
      message: "删除成功",
    };
  } else {
    ctx.body = {
      code: 40000,
      message: "删除id不存在",
    };
  }
});

// 修改
/**
 * @swagger
 * /room/list:
 *   post:
 *     summary: 修改房屋列表
 *     description: 修改房屋列表
 *     tags: [房屋管理]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         in: formData
 *         required: true
 *         description: id
 *         type: string
 *       - name: name
 *         in: formData
 *         required: false
 *         description: 小区名称
 *         type: string
 *       - name: title
 *         in: formData
 *         required: false
 *         description: 小区的描述
 *         type: string
 *       - name: size
 *         in: formData
 *         required: false
 *         description: 房屋大小
 *         type: string
 *       - name: price
 *         in: formData
 *         required: false
 *         description: 房屋价格
 *         type: string
 *       - name: person
 *         in: formData
 *         required: false
 *         description: 按照姓名搜索  "李兰", "黄明", "昭武", "李四"
 *         type: string
 *       - name: state
 *         in: formData
 *         required: false
 *         description: 1 已经出售  2 未出售
 *         type: string
 *       - name: level
 *         in: formData
 *         required: false
 *         description: 选项：毛坯, 精装, 简装
 *         type: string
 *       - name: sizeRoom
 *         in: formData
 *         required: false
 *         description: 选项： 一居 ，两居，三居
 *         type: string
 *     responses:
 *       10000:
 *         description: 成功获取
 */
router.post("/room/list", (ctx) => {
  const body = ctx.request.body;
  const item = roomList.find((v) => v.id === body.id);
  if (item) {
    Object.assign(item, body);
    ctx.body = {
      code: 0,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      code: 1,
      message: "修改失败",
    };
  }
});


// 修改
/**
 * @swagger
 * /room/list/edit:
 *   post:
 *     summary: 修改房屋列表(需要权限)
 *     description: 修改房屋列表
 *     tags: [房屋管理]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: id
 *         in: formData
 *         required: true
 *         description: id
 *         type: string
 *       - name: name
 *         in: formData
 *         required: false
 *         description: 小区名称
 *         type: string
 *       - name: title
 *         in: formData
 *         required: false
 *         description: 小区的描述
 *         type: string
 *       - name: size
 *         in: formData
 *         required: false
 *         description: 房屋大小
 *         type: string
 *       - name: price
 *         in: formData
 *         required: false
 *         description: 房屋价格
 *         type: string
 *       - name: person
 *         in: formData
 *         required: false
 *         description: 按照姓名搜索  "李兰", "黄明", "昭武", "李四"
 *         type: string
 *       - name: state
 *         in: formData
 *         required: false
 *         description: 1 已经出售  2 未出售
 *         type: string
 *       - name: level
 *         in: formData
 *         required: false
 *         description: 选项：毛坯, 精装, 简装
 *         type: string
 *       - name: sizeRoom
 *         in: formData
 *         required: false
 *         description: 选项： 一居 ，两居，三居
 *         type: string
 *     responses:
 *       10000:
 *         description: 成功获取
 */
router.post("/room/list/edit", (ctx) => {
  const body = ctx.request.body;
  const item = roomList.find((v) => v.id === body.id);
  if (item) {
    Object.assign(item, body);
    ctx.body = {
      code: 0,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      code: 1,
      message: "修改失败",
    };
  }
});

// 添加
/**
 * @swagger
 * /room/list:
 *   put:
 *     summary: 添加房屋列表
 *     description: 添加房屋列表
 *     tags: [房屋管理]
 *     produces:
 *       - application/x-www-form-urlencoded
 *     parameters:
 *       - name: name
 *         in: formData
 *         required: false
 *         description: 小区名称
 *         type: string
 *       - name: title
 *         in: formData
 *         required: false
 *         description: 小区的描述
 *         type: string
 *       - name: size
 *         in: formData
 *         required: false
 *         description: 房屋大小
 *         type: string
 *       - name: price
 *         in: formData
 *         required: false
 *         description: 房屋价格
 *         type: string
 *       - name: person
 *         in: formData
 *         required: false
 *         description: 按照姓名搜索  "李兰", "黄明", "昭武", "李四"
 *         type: string
 *       - name: state
 *         in: formData
 *         required: false
 *         description: 1 已经出售  2 未出售
 *         type: string
 *       - name: level
 *         in: formData
 *         required: false
 *         description: 选项：毛坯, 精装, 简装
 *         type: string
 *       - name: sizeRoom
 *         in: formData
 *         required: false
 *         description: 选项： 一居 ，两居，三居
 *         type: string
 *     responses:
 *       10000:
 *         description: 成功获取
 */
router.put("/room/list", (ctx) => {
  const body = ctx.request.body;
  roomList.unshift({
    ...body,
    id: Mock.Random.id(),
  });
  ctx.body = {
    code: 0,
    message: "添加成功",
  };
});


module.exports = router;
